Imports Microsoft.VisualBasic
Imports System
Imports DevExpress.Web.ASPxUploadControl
Imports System.Web.UI
Imports System.IO
Imports DevExpress.Web
Imports System.Drawing

Imports System.Data.Common
Imports System.Data
Imports System.Data.SqlClient
Imports BusinessLogic.crFichaInscripcion
Partial Class Pages_frmCargarFoto
    'Inherits System.Web.UI.Page
    Inherits Page
    Const UploadDirectory As String = "~/UploadImages/"
    Const FotoDirectory As String = "~/Foto/"
    Const ThumbnailSize As Integer = 100

    Protected Function SavePostedFile(ByVal uploadedFile As UploadedFile) As String
        Dim ret As String = ""
        If uploadedFile.IsValid Then
            Dim tempFileName As String = MapPath(UploadDirectory) + Path.GetRandomFileName()
            Dim fileName As String = Convert.ToString(DBUtilities.TitularID) + ".jpg"
            Dim resFileName As String = MapPath(UploadDirectory) & fileName
            uploadedFile.SaveAs(tempFileName)
            PhotoUtils.CreateThumbnailFile(tempFileName, ThumbnailSize, resFileName)
            File.Delete(tempFileName)
            ret = fileName
            System.Web.HttpContext.Current.Session.Add("RutaFoto", resFileName)
        End If
        Return ret
    End Function
    Protected Sub uplImage_FileUploadComplete(ByVal sender As Object, ByVal e As FileUploadCompleteEventArgs) Handles uplImage.FileUploadComplete
        e.CallbackData = SavePostedFile(e.UploadedFile)
    End Sub


    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click

    End Sub

    Protected Sub btnGrabar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGrabar.Click

        GrabarFotoTitular()


    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LlenarDatos()
    End Sub

    Sub LlenarDatos()
        Dim objBase As New BusinessLogic.crConsulta
        txtTitularID.Text = DBUtilities.TitularID
        lblNombreTitular.Text = objBase.Buscar_Campo(DBUtilities.TitularID, "TB_SOC_ESI_Titular_ID", "NombreCompleto", "viewDatosPersonales", "", "", "")

    End Sub

    Sub GrabarFotoTitular()
        Dim objGrabar As New BusinessLogic.crFichaInscripcion
        Try
            ''Dim tempFileName As String = DBUtilities.GetRutaFoto
            ''Dim fileName As String = Convert.ToString(DBUtilities.TitularID) + ".jpg"
            ''Dim resFileName As String = MapPath(FotoDirectory) & fileName
            ''PhotoUtils.CreateThumbnailFile(tempFileName, ThumbnailSize, resFileName)
            ''File.Delete(tempFileName)

            If objGrabar.GrabarRutaFoto(DBUtilities.TitularID, DBUtilities.GetRutaFoto) = 1 Then
                lblMensaje.Visible = True
                lblMensaje.Text = "Grabo..."

                CerrarPantalla()

            Else
                lblMensaje.Visible = True
                lblMensaje.Text = "No Grabo"
            End If


        Catch ex As System.IO.FileNotFoundException
            lblMensaje.Visible = True
            lblMensaje.Text = "No Grabo" 'ex.Message


        Catch ex As Exception

            lblMensaje.Visible = True
            lblMensaje.Text = "No Grabo" 'ex.Message

        End Try
    End Sub
    Public Sub CerrarPantalla()
        Response.Write("<script language='javascript'> {window.opener.location.reload(); window.close();}</script>")
        Me.Dispose()
    End Sub

    Public Sub CancelarPantalla()
        Response.Write("<script language='javascript'> { window.close();}</script>")
        Me.Dispose()
    End Sub
    Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelar.Click

        CancelarPantalla()
    End Sub
End Class

